package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.model.entity.PaymentType;
import com.atguigu.lease.web.admin.mapper.AttrKeyMapper;
import com.atguigu.lease.web.admin.mapper.AttrValueMapper;
import com.atguigu.lease.web.admin.mapper.RoomAttrValueMapper;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.RoomAttrValue;
import com.atguigu.lease.web.admin.service.RoomAttrValueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_attr_value(房间&基本属性值关联表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomAttrValueServiceImpl extends ServiceImpl<RoomAttrValueMapper, RoomAttrValue>
        implements RoomAttrValueService {

    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    AttrKeyMapper attrKeyMapper;

    @Override
    public List<AttrValueVo> getAttrValueVo(Long id) {
        List<RoomAttrValue> list = super.list(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        List<AttrValue> attrValues = attrValueMapper.
                selectList(new LambdaQueryWrapper<AttrValue>()
                        .in(AttrValue::getId, list.stream().
                                map(RoomAttrValue::getAttrValueId)
                                .collect(Collectors.toList())));
        ArrayList<AttrValueVo> attrValueVos = new ArrayList<>();
        attrValues.forEach(attrValue -> {
            AttrValueVo attrValueVo = new AttrValueVo();
            AttrKey attrKey = attrKeyMapper.selectById(attrValue.getAttrKeyId());
            String name = attrKey.getName();
            BeanUtils.copyProperties(attrValue, attrValueVo);
            attrValueVo.setAttrKeyName(name);
            attrValueVos.add(attrValueVo);
        });

        return attrValueVos;
    }


}




